﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace SubstrateHelper.Helpers
{
    public class HttpHelper
    {
        public static string Post(string addr,string content,bool isXml=false)
        {
            // 创建链接
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(addr);
            request.Method = "post";
            byte[] Lastdata = Encoding.UTF8.GetBytes(content);
            request.ContentLength = Lastdata.Length;            
            if (isXml)
            {
                request.ContentType = "text/xml";
            }
            else
            {
                request.ContentType = "application/x-www-form-urlencoded";//multipart/form-data
                //request.ContentType = "multipart/form-data";//multipart/form-data
            }
            Stream newStream = request.GetRequestStream();            
            newStream.Write(Lastdata, 0, Lastdata.Length);

            //开始请求，并得到返回值
            HttpWebResponse res = request.GetResponse() as HttpWebResponse;
            StreamReader srder = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
            String ret = srder.ReadToEnd();
            newStream.Close();
            return ret;
        }
    }
}
